package com.deyuanyun.pic.settings.support.shiro;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.settings.domain.prvlg.Menu;
import com.deyuanyun.pic.settings.service.prvlg.MenuService;
import com.deyuanyun.pic.settings.support.cache.TempStoreDataSupport;
import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by xu on 2015/12/22.
 * 处理shiro权限相关
 */
public class ShiroUrlUtils {
    public static String getPerString(){
        //将菜单查出来放入缓存
        MenuService menuService= SpringAppliction.getBean(MenuService.class);
        List<Menu> menuList = menuService.querySameLevelMenu("","");
        /*ArrayListMultimap menuListMultimap= ArrayListMultimap.create();
        for (Menu menu : menuList){
            if (StringUtils.isBlank(menu.getParentId())){
                menu.setParentId("0");
            }
            menuListMultimap.put(menu.getParentId(),menu);
        }*/
        TempStoreDataSupport.pushDataf("application.menus",menuList);

        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("/login.html = authc\n");
        stringBuilder.append("/clientLogin.html = anon\n");
        stringBuilder.append("/noPerError.jsp = anon\n");
        stringBuilder.append("/settings/thresholdInteface/** = anon\n");
        stringBuilder.append("/greatwall/** = anon\n");
        stringBuilder.append("/comm/UploadFile.json = anon\n");//TODO 未来：主要是客户端要用上传文件，按理说是需要权限的，只是现在未做。
        stringBuilder.append("/comm/downNetFile.json = anon\n");//TODO 未来：主要是导出PDF中的地图是跨域的。
        stringBuilder.append("/pbf/app/** = anon\n");
        stringBuilder.append("/ctr/app/** = anon\n");
        stringBuilder.append("/error.jsp = anon\n");
        stringBuilder.append("/image/** = anon\n");
        stringBuilder.append("/app/** = anon\n");
        stringBuilder.append("/loginout.html = logout\n");
        stringBuilder.append("/upload/** = user\n");
        stringBuilder.append("/menu/** = user\n");
        stringBuilder.append("/comm/** = user\n");
        stringBuilder.append("/toHome.html = user\n");
        stringBuilder.append("/getUserMsg.json = user\n");

        stringBuilder.append("/monitoring = authc,perms[admin:manage]\n");
        for (Menu menu:menuList){
           // if (menu.getSort()==null){continue;}
            if (StringUtils.isBlank(menu.getUrl())){continue;}
            String url=menu.getUrl();
            int i_ = StringUtils.countMatches(url,"/");
            if (i_ > 2) {
                url=StringUtils.substringBeforeLast(menu.getUrl(),"/");
                stringBuilder.append(url+"/** = user,sperm[\"").append(menu.getCode()).append("\",\"admin:manage").append("\"]\n");
            }else {
                url=StringUtils.substringBeforeLast(menu.getUrl(),"/");
                //stringBuilder.append(url+" = authc,sperm[\"").append(menu.getCode()).append("\",\"admin:manage").append("\"]\n");
                stringBuilder.append(url+"/* = user,sperm[\"").append(menu.getCode()).append("\",\"admin:manage").append("\"]\n");
            }

            //stringBuilder.append(url+"/** = authc,perms[\"").append(menu.getCode()).append(",admin:manage").append("\"]\n");

        }
        stringBuilder.append("/** = user,sperm[admin:manage]\n");
        return stringBuilder.toString();
    }


}
